package Thread;

import java.util.concurrent.locks.*;

/**
 * @description:
 * @title: LockSupportDemo
 * @author: zp
 * @date: 2021/8/17 20:35
 * LockSupport : 帮助我们更好的调度线程的加强类（wait/notify） 唤醒和休眠
 * 休眠和唤醒线程三种方式：后两种需要成对出现 前后顺序不能弄乱 否则无法生效
 * 1、Lock condition.await()/condition.signal()
 * 2、Object wait/notify
 * 3、LockSupport pack/unpack
 */
public class LockSupportDemo {
    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock();
        ReentrantReadWriteLock lock1 = new ReentrantReadWriteLock();
        lock1.readLock();
        lock1.writeLock();
        Condition condition = lock.newCondition();
        condition.await();
        condition.signal();
        Thread thread = new Thread(() -> {
            System.out.println("我死了");
            //阻塞线程
            LockSupport.park();
            System.out.println("我活过来了");
        });
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //System.out.println(LockSupport.getBlocker(thread));
            //释放线程
            LockSupport.unpark(thread);
            System.out.println("我去叫醒他");
        });
        thread.start();
        thread1.start();
    }
}
